﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using BSU.CSCE.Data.Model;
using BSU.CSCE.Utilities;

namespace BSU.CSCE.Data.Repositories
{
    public class CSCEDataContext : DataContext
    {
        #region properties

        public Table<User> Users { get { return GetTable<User>(); } }
        public Table<Publication> Publications { get { return GetTable<Publication>(); } }
        
        #endregion

        #region constructors

        public CSCEDataContext()
            : this(new Configuration().DBConnectionString)
        {
        }

        public CSCEDataContext(DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
            : this(new Configuration().DBConnectionString, dlo, useDefaultDataLoadingOptions)
        {
        }


        public CSCEDataContext(string connectionString)
            : base(connectionString)
        {
            InitializeContext(null, true);
        }

        public CSCEDataContext(string connectionString, DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
            : base(connectionString)
        {
            InitializeContext(dlo, useDefaultDataLoadingOptions);
        }

        public CSCEDataContext(string connectionString, XmlMappingSource map)
            : base(connectionString, map)
        {
            InitializeContext(null, true);
        }

        #endregion

        #region methods

        private void InitializeContext(DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
        {
            if (dlo == null) dlo = new DataLoadOptions();
            if (useDefaultDataLoadingOptions)
            {
                //dlo.LoadWith<User>(x => x.Status);
            }
            LoadOptions = dlo;
        }

        //public override void SubmitChanges(ConflictMode failureMode)
        //{
        //    using (new LinqToSQLProfiller(this, true))
        //    {
        //        base.SubmitChanges(failureMode);
        //    }

        //}

        //protected internal new IExecuteResult ExecuteMethodCall(object instance, MethodInfo methodInfo, params object[] parameters)
        //{
        //    using (new LinqToSQLProfiller(this, true))
        //    {
        //        return base.ExecuteMethodCall(instance, methodInfo, parameters);
        //    }
        //}

        #endregion

    }
}
